/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketManager.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Named;
import javax.persistence.EntityManager;
import ticketManager.ejb.HistoryDAO;
import ticketManager.ejb.ProblemDAO;
import ticketManager.ejb.ProductDAO;
import ticketManager.mailSenderServlet.TimerEmail;
import ticketManager.model.ManagerUser;
import ticketManager.model.Problem;
import ticketManager.model.ProblemStates;
import ticketManager.model.Product;

/**
 *
 * @author Administrator
 */
@Named
@RequestScoped
public class CreateProblemController {

    @EJB
    ProblemDAO pDAO;
    @EJB
    HistoryDAO hDAO;
    @EJB
    ProductDAO proDAO;

    EntityManager em;

    private String title;
    private String description;

    //private List<SelectItem> selectItemList;
    private Product select_product;
    private String select_product_title;
    private List<Product> products = new ArrayList<>();
    private ArrayList<String> products_title = new ArrayList<>();

    public ProblemDAO getpDAO() {
        return pDAO;
    }

    public void setpDAO(ProblemDAO pDAO) {
        this.pDAO = pDAO;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Product getSelect_product() {
        return select_product;
    }

    public void setSelect_product(Product select_product) {
        this.select_product = select_product;
    }

    public String getSelect_product_title() {
        return select_product_title;
    }

    public void setSelect_product_title(String select_product_title) {
        this.select_product_title = select_product_title;
    }

    public List<Product> getProducts() {
        products = proDAO.getAllProduct();
        return products;
    }

    public ArrayList<String> getProducts_title() {
        products = proDAO.getAllProduct();
        Iterator i = products.iterator();
        while (i.hasNext()) {
            products_title.add(((Product) i.next()).getTitle());
        }
        return products_title;
    }

    public List<SelectItem> getSelectItemList() {
        List<SelectItem> selectItemList1 = new ArrayList<>();
        products = proDAO.getAllProduct();
        Iterator i = products.iterator();
        while (i.hasNext()) {
            selectItemList1.add(new SelectItem((Product) i.next(), ((Product) i.next()).getTitle()));
        }
        return selectItemList1;
    }

//    public void listProducts()
//    {
//        products = proDAO.getAllProduct();
//    }
    public String doCreateProblem() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ManagerUser m = (ManagerUser) facesContext.getExternalContext().getSessionMap().get("user");
        TimerEmail t = new TimerEmail();
        if (!(title.equals("")) && !(description.equals(""))) {
            Product pro1 = proDAO.getProductByTitle(select_product_title);
            Problem pro = pDAO.createProblem(title, description, m, pro1);
            title = "";
            description = "";
            products_title.clear();
            //hDAO.createProblemEvent(m, pro);
            t.timersend();
            return "success";
        } else {
            return "error";
        }
    }
}
